package com.base.entity;

import java.util.ArrayList;
import java.util.List;

public class TreeNode implements Comparable<TreeNode>{
    /**
     * 节点名称
     */
    private String name;
    /**
     * 计数
     */
    private Integer count;
    /**
     * 父节点
     */
    private TreeNode parent;
    /**
     * 子节点
     */
    private List<TreeNode> children;
    /**
     * 下一个有相同节点的
     */
    private TreeNode sameNext;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public TreeNode getSameNext() {
        return sameNext;
    }

    public void setSameNext(TreeNode sameNext) {
        this.sameNext = sameNext;
    }



    /**
     * 添加一个节点
     * @param child
     */
    public void addChild(TreeNode child) {
        if (this.getChildren() == null) {
            List<TreeNode> list = new ArrayList<TreeNode>();
            list.add(child);
            this.setChildren(list);
        } else {
            this.getChildren().add(child);
        }
    }
    /**
     *  是否存在着该节点,存在返回该节点，不存在返回空
     * @param name
     * @return
     */
    public TreeNode findChild(String name) {
        List<TreeNode> children = this.getChildren();
        if (children != null) {
            for (TreeNode child : children) {
                if (child.getName().equals(name)) {
                    return child;
                }
            }
        }
        return null;
    }

    @Override
    public int compareTo(TreeNode args) {
        int count0 = args.getCount();
        // 跟默认的比较大小相反，导致调用Arrays.sort()时是按降序排列
        return count0 - this.count;
    }
}
